//
//  SystemVoiceItemView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/18.
//

import SwiftUI

//系统音色 item
struct SystemVoiceItemView: View {
    @State var showChoose = false
    @StateObject var viewModel = SystemVoiceItemViewModel()
    var item: AiVoiceBean
    var choose = false
    let onChoose: (AiVoiceBean) -> Void

    var body: some View {
        Menu {
            commonMenuContent
        } label: {
            HStack{
                Text(item.name ?? "")
                    .font(.system(size: 16))
                    .foregroundStyle(.bgNormalText2)
                Spacer()
                if (item.voiceId ?? "")?.isEmpty == false {
                    Image(systemName: "waveform")
                        .resizable()
                        .frame(width: 20,height: 20)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            viewModel.doVoice(bean: item)
                        }
                }
            }
        }.padding(.vertical, 10)
            .padding(.horizontal, 20)
            .contentShape(Rectangle())
            .contextMenu{
                commonMenuContent
            }.alert("温馨提示",isPresented:$showChoose) {
                Button("取消",role: .cancel){}
                Button("确定",role: .destructive) {
                    onChoose(item)
                }
            } message: {
                Text("是否确定选择该音色")
            }
    }
    
}

//菜单
extension SystemVoiceItemView {
    // 将菜单内容抽离为一个计算属性，方便复用
    private var commonMenuContent: some View {
        Group {
            Button("试听") {
                if (item.voiceId ?? "")?.isEmpty == true {
                    showTipsDialog(text: "当前音色还没制作完成")
                    return
                }
                viewModel.doVoice(bean: item)
            }
            if choose {
                Button("选择") {
                    showChoose.toggle()
                }
            }
        }
    }
    
}

#Preview {
    SystemVoiceItemView(item: AiVoiceBean()){_ in }
}
